const nums = [1, 2, 3];

const result = nums.filter((value, index, array) => {
    return value > 2;
});

console.log('result: ', result);

Array.prototype.filter = function(fn) {
    const result = [];
    const self = this; 

    for (let i = 0; i < self.length; i++) {
        if (fn(self[i], i, self)) {
            result.push(self[i]);
        }
    }

    return result;
}

function throttle (fn, time) {
    let flag = true;

    return (...args) => {
        if (flag) {
            flag = false;
            fn(...args);
            setTimeout(() => {
                flag = true;
            }, time);
        }
    }
}

function throttle2 (fn, time) {
    let flag = true;
    let taskQueue = [];

    const tempFn = (...args) => {
        setTimeout(() => {
            fn(...args);
            if (taskQueue.length) {
                tempFn(...taskQueue[taskQueue.length - 1]);
                taskQueue.length = 0;
            }
        }, time);
    }

    return (...args) => {
        taskQueue.push([...args]);
        tempFn(...args);
    }
}